﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ExpenseSystem.Core;
using ExpenseSystem.Core.Authority;
using ExpenseSystem.Fether;
using ExpenseSystem.Common;
using ExpenseSystem.Lor;
using ExpenseSystem.Model;
using ExpenseSystem.Common.BaseModel;

namespace ExpenseSystem.Controllers
{
    [RoutePrefix("api/Modules")]
    public class ModulesController : ApiController
    {
        SetInhibition setInhibition;
        GetInhibition getInhibition;
        SetInhibitionMaker inhibitionMaker = new StateInhibitionMaker();
        GetInhibitionMaker getInhibitionMaker = new GetInhibitionMaker();
        public ModulesController()
        {
            setInhibition = inhibitionMaker.Make();
            getInhibition = getInhibitionMaker.Make();
        }
        ModuleFetcher fetcher = new ModuleFetcher();

        [Route("ListAdmin")]
        [HttpGet]
        public ResponseModel ListAdmin()
        {
            ResponseModel responseModel = new ResponseModel();
            Authority authority = new Authority();
            if (!authority.GetAuthority("SYS_MODULE_LIST"))
            {
                responseModel.Result = Definition.RESP_NOT_AUTH;
                responseModel.Count = 0;
                responseModel.Data = null;
                responseModel.Message = "没有授予对应的权限";
                return responseModel;
            }
            ModuleFetcher moduleFetcher = new ModuleFetcher();

            
            string message;
            int rows;
            IList<ModuleModel> result = moduleFetcher.ListAdmin(out message,out rows);
            responseModel.Count = rows;
            responseModel.Data = JsonHelper.ArrayToJson(result, getInhibition);
            responseModel.Message = message;
            return responseModel;
        }
        [Route("ListNormal")]
        [HttpGet]
        public ResponseModel ListNormal()
        {
            ResponseModel responseModel = new ResponseModel();
            Authority authority = new Authority();
            ModuleFetcher moduleFetcher = new ModuleFetcher();
            string message;
            int rows;
            IList<ModuleModel> result = moduleFetcher.ListNormal( out message,out rows);
            responseModel.Count = rows;
            responseModel.Data = JsonHelper.ArrayToJson(result, getInhibition);
            responseModel.Message = message;
            return responseModel;
        }
        [Route("ListMenu")]
        [HttpGet]
        public ResponseModel ListMenu()
        {
            ResponseModel responseModel = new ResponseModel();
            Authority authority = new Authority();
            ModuleFetcher moduleFetcher = new ModuleFetcher();
            string message;
            int rows;
            IList<ModuleModel> tmp = moduleFetcher.ListMenu(out message,out rows);
            /*
            List<ModuleModel> result = new List<ModuleModel>();
            foreach(ModuleModel moduleModel in tmp)
            {
                RoleCmdFetcher roleCmdFetcher = new RoleCmdFetcher();
                IList<RoleCmdModel> roleCmdModels = roleCmdFetcher.ListByModule(moduleModel.AutoId,out message,out rows);
                bool hit = false;
                foreach(RoleCmdModel r in roleCmdModels)
                {
                    if (authority.GetAuthority(r.Code))
                    {
                        result.Add(moduleModel);
                        hit = true;
                        break;
                    }
                }
                if (hit)
                    continue;
                
            }*/

            responseModel.Count = rows;
            responseModel.Data = JsonHelper.ArrayToJson(tmp, getInhibition);
            responseModel.Message = message;
            return responseModel;
        }
        [Route("List")]
        [HttpGet]
        public ResponseModel ListAll()
        {
            return SystemControllerHelper.List(fetcher, "",null, getInhibition);
        }
    }
}
